package com.service.calculate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.service.bean.Road_Data;
import com.service.bean.Road_GpsSignal;
import com.service.other.Logger;

/**
 * 基础值计算
 * @author sxc
 *
 */
public class Road_BasalCalculator {
	
	/**
	 * 基础值计算
	 * @param data
	 * @param signal
	 */
	public void calculate(Road_Data data,Road_GpsSignal signal){		
		signal.gpsTime = this.getGpsTime(data);
		signal.lat = new BigDecimal(Long.parseLong(data.latitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
		signal.lng = new BigDecimal(Long.parseLong(data.longitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
		
		signal.direction = Integer.parseInt(data.direction,16) / 10.00;
		signal.curMile = Integer.parseInt(data.mileage, 16) / 10.00;
	}
	
	/**
	 * GPS时间
	 */
	private Date getGpsTime(Road_Data data) {
		StringBuilder builder = new StringBuilder();
		Date date = null;
		try {
			builder.append(2000+Integer.parseInt(data.year, 16));
			builder.append("-");
			builder.append(Integer.parseInt(data.month, 16));
			builder.append("-");
			builder.append(Integer.parseInt(data.day, 16));
			builder.append(" ");
			builder.append(Integer.parseInt(data.hour, 16));
			builder.append(":");
			builder.append(Integer.parseInt(data.minute, 16));
			builder.append(":");
			builder.append(Integer.parseInt(data.second, 16));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(builder.toString());
		}catch(Exception e){
			Logger.err(e);
		}
		Date now =  new Date();
		
		if(date != null && date.getTime() - now.getTime() > 5*60*1000){
			return now;
		}else {
			return date;
		}
	}
}
